package org.dolphin.entity.security;

import java.io.Serializable;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.util.ObjectUtils;

@Entity
@Table(name = "MS_USER_GROUP")
@AssociationOverrides(value = {
    @AssociationOverride(name = "pk.user", joinColumns = @JoinColumn(name = "USER_ID")),
    @AssociationOverride(name = "pk.group", joinColumns = @JoinColumn(name = "GROUP_ID"))})
public class UserGroup implements Serializable {
  private static final long serialVersionUID = 1182810081485845201L;
  private UserGroupPk pk = new UserGroupPk();

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof UserGroup) {
      UserGroup oth = (UserGroup) obj;
      return ObjectUtils.nullSafeEquals(getPk(), oth.getPk());
    }
    return false;
  }

  @Transient
  public Group getGroup() {
    return getPk().getGroup();
  }

  @EmbeddedId
  public UserGroupPk getPk() {
    return pk;
  }

  @Transient
  public User getUser() {
    return getPk().getUser();
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getPk());
    return hash;
  }

  public void setGroup(Group group) {
    getPk().setGroup(group);
  }

  public void setPk(UserGroupPk pk) {
    this.pk = pk;
  }

  public void setUser(User user) {
    getPk().setUser(user);
  }
}
